// <copyright file="ReturnInstruction.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using System;
    using System.Collections.Generic;
    using Eco.ObjectRepresentation;
    using Eco.UmlCodeAttributes;

    /// <summary>
    /// ReturnInstruction: Instruction that return a value to the caller
    /// </summary>
    public partial class ReturnInstruction
    {
        /// <summary>
        /// Executes the function.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="variableList">The variable list.</param>
        /// <returns>Return value of executed function</returns>
        [UmlElement(Id = "c316ca33-1e6b-4fbb-a6ab-ced017ae0524")]
        protected internal override IElement ExecuteFunction(IEcoServiceProvider serviceProvider, IModifiableVariableList variableList)
        {
            ReturnElement returnElement = new ReturnElement(this.ServiceProvider(), this.OCLInstruction.ExecuteFunction(serviceProvider, variableList));
            return returnElement.AsIObject().Element;
        }

        /// <summary>
        /// Inits the specified tokens.
        /// </summary>
        /// <param name="tokens">The tokens.</param>
        [UmlElement(Id = "4fb430ca-d39a-4fae-bf27-e903376c1e69")]
        protected internal override void Init(Dictionary<IList<EslToken>, Type> tokens)
        {
            List<EslToken> tokenList = new List<EslToken>();
            tokenList.Add(new EslToken(EslTokenType.Word, "Return"));
            tokens.Add(tokenList, this.GetType());
        }

        /// <summary>
        /// Parses the specified token list.
        /// </summary>
        /// <param name="tokenList">The token list.</param>
        /// <param name="ecoScript">The eco script.</param>
        /// <param name="fullExpression">if set to <c>true</c> [full expression].</param>
        [UmlElement(Id = "08e4586c-d9c9-4879-8d6b-28881afa6a37")]
        protected internal override void Parse(IList<EslToken> tokenList, EcoScript ecoScript, bool fullExpression)
        {
            EslToken token = GetNextToken(tokenList);
            RememberEcoScript(ecoScript, token);

            EslInstruction instruction = ecoScript.ReadNextInstruction(tokenList, fullExpression);

            if (!(instruction is InstructionWithOcl))
            {
                throw new EslException("Instruction with OCL expected at " + token.ToString(), token);
            }

            this.OCLInstruction = instruction as InstructionWithOcl;
        }
    }
}